package com.holyn.guofang.view.distinguish;

import android.app.Activity;
import android.content.Intent;
import android.text.TextUtils;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.holyn.guofang.R;
import com.holyn.guofang.model.constant.AlgorithmTypeConstant;
import com.holyn.guofang.view.BaseActivity;
import com.holyn.guofang.viewcommon.popupwindow.PopupSingleSelectFromBottom;
import com.vondear.rxtools.RxRegUtils;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

/**
 * Created by Holyn on 2017/7/27.
 */

public class QueryContentAddActivity extends BaseActivity {


    public static final String INTENT_KEY_SELECT_TYPE = "INTENT_KEY_SELECT_TYPE";
    public static final String INTENT_KEY_SELECT_CONTENT = "INTENT_KEY_SELECT_CONTENT";

    public static final int REQUEST_CODE_IMAGE_ELEMENT = 0x0001;

    @BindView(R.id.tv_type_select)
    TextView tvTypeSelect;
    @BindView(R.id.et_type_content)
    EditText etTypeContent;
    @BindView(R.id.btn_select_img_element)
    Button btnSelectImgElement;
    @BindView(R.id.ll_select_img_element)
    LinearLayout llSelectImgElement;
    @BindView(R.id.btn_submit)
    Button btnSubmit;

    String CnContent = "";
    String EnContent = "";
    String ImageCode = "";

    private PopupSingleSelectFromBottom popupSelectType;
    private List<String> typeNameList = new ArrayList<>();
    private int curSelectPosition = 0;

    public static void startForResult(Activity fromActivity, int requestCode){
        Intent intent = new Intent(fromActivity, QueryContentAddActivity.class);
        fromActivity.startActivityForResult(intent, requestCode);
    }

    @Override
    protected int getLayoutResID() {
        return R.layout.activity_condition_select;
    }

    @Override
    protected void onMyCreate() {
        ButterKnife.bind(this);
        showToolbar("添加查询的内容");

        for (String type: AlgorithmTypeConstant.TYPE_VALUE_ARRAY_DISTINGUISH){
            typeNameList.add(type);
        }

        popupSelectType = new PopupSingleSelectFromBottom(this, "请选择", typeNameList);
        popupSelectType.setOnItemClickListener(new PopupSingleSelectFromBottom.OnItemClickListener() {
            @Override
            public void onItemClick(String item, int position) {
                tvTypeSelect.setText(typeNameList.get(position));
                curSelectPosition = position;
                if (position == 2){
                    llSelectImgElement.setVisibility(View.VISIBLE);
                    etTypeContent.setEnabled(false);
                    etTypeContent.setHint("请点击下边按钮");
                } else {
                    llSelectImgElement.setVisibility(View.INVISIBLE);
                    etTypeContent.setEnabled(true);
                    etTypeContent.setHint("请输入查询内容");
                }
            }
        });

    }
    @OnClick({R.id.tv_type_select, R.id.btn_select_img_element, R.id.btn_submit})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.tv_type_select:
                popupSelectType.showPopupWindow();
                break;
            case R.id.btn_select_img_element:
                ImageElementActivity.startForResult(QueryContentAddActivity.this, REQUEST_CODE_IMAGE_ELEMENT);
                break;
            case R.id.btn_submit:
                String content = etTypeContent.getText().toString().trim();
                if (TextUtils.isEmpty(content)){
                    toastMsg("请输入查询内容");
                } else {
                    if (curSelectPosition == 0){
                        if (!RxRegUtils.isChz(content)){
                            toastMsg("查询内容格式错误");
                            return;
                        }
                    } else if (curSelectPosition == 1){
                        if (!RxRegUtils.isEn(content)){
                            toastMsg("查询内容格式错误");
                            return;
                        }
                    }
                    String type = AlgorithmTypeConstant.TYPE_ARRAY_DISTINGUISH[curSelectPosition];
                    Intent intent = getIntent();
                    intent.putExtra(INTENT_KEY_SELECT_TYPE, type);
                    intent.putExtra(INTENT_KEY_SELECT_CONTENT, content);
                    setResult(Activity.RESULT_OK, intent);
                    QueryContentAddActivity.this.finish();
                }
                break;
        }
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (resultCode == Activity.RESULT_OK){
            switch (requestCode){
                case REQUEST_CODE_IMAGE_ELEMENT:
                    ImageCode = data.getStringExtra(ImageElementActivity.INTENT_KEY_IMAGE_CODES);
                    etTypeContent.setText(ImageCode);
                    break;
            }
        }
    }
}
